package com.yami.shop.api.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.model.Bank;
import com.yami.shop.bean.model.Carry;
import com.yami.shop.bean.model.User;
import com.yami.shop.dao.BankMapper;
import com.yami.shop.dao.CarryMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/p/carry")
@Api(tags = "提现接口")
public class CarryController {

    @Autowired
    private CarryMapper carryMapper;
    @Autowired
    private BankMapper bankMapper;
    @Autowired
    private UserMapper userMapper;

    @PostMapping("/bindingBank")
    @ApiOperation(value = "绑定银行卡信息",notes = "绑定银行卡信息")
    public ResponseEntity<String> bindingBank(@RequestBody Bank bankInFo){
        String userId = SecurityUtils.getUser().getUserId();
        bankInFo.setUserId(userId);
        Date now = new Date();
        bankInFo.setState(1);
        bankInFo.setUppTime(now);
        int num = bankMapper.selectCount(new QueryWrapper<Bank>()
                .eq("Bank_card",bankInFo.getBankCard())
                .eq("user_id",bankInFo.getUserId()));
        if(num > 0){
            return ResponseEntity.ok("此银行卡已被绑定");
        }
        bankMapper.insert(bankInFo);
        return ResponseEntity.ok("绑定成功");
    }

    @GetMapping("/delBank")
    @ApiOperation(value = "解除绑定银行卡",notes = "解除绑定银行卡")
    public ResponseEntity<String> delBank(String card){
        bankMapper.delete(new QueryWrapper<Bank>().eq("bank_card",card));
        return ResponseEntity.ok().build();
    }
    @GetMapping("/queryBank")
    @ApiOperation(value = "查询银行卡列表",notes = "查询银行卡列表")
    public ResponseEntity<List<Bank>> queryBank(){
        String userId = SecurityUtils.getUser().getUserId();
        List<Bank> banks = bankMapper.selectList(new QueryWrapper<Bank>().eq("user_id",userId));
        return ResponseEntity.ok(banks);
    }

    @PostMapping("/carryRMB")
    @ApiOperation(value = "提现",notes = "用户提现")
    public ResponseEntity<String> carryMoney(@RequestBody Carry carry){
        String userId = SecurityUtils.getUser().getUserId();
        User user = userMapper.selectById(userId);
        if(carry.getRmb() > user.getWallet()){
            return ResponseEntity.badRequest().body("最多可提现"+user.getWallet()+"元");
        }
        if(carry.getRmb() % 300 != 0){
            return ResponseEntity.badRequest().body("只能提现300的整数倍哦");
        }
        Date now = new Date();
        carry.setUserId(userId);
        carry.setCarryTime(now);
        carry.setState(3);
        Double num = user.getWallet() - carry.getRmb();
        user.setWallet(num);
        userMapper.updateById(user);
        carryMapper.insert(carry);
        return ResponseEntity.ok("发起提现成功");

    }

    @GetMapping("/queryCarry")
    @ApiOperation(value = "提现记录",notes = "提现记录")
    public ResponseEntity<List<Carry>> queryCarry(){
        String userId = SecurityUtils.getUser().getUserId();
        List<Carry> lists = carryMapper.selectList(new QueryWrapper<Carry>().eq("user_id",userId));
        return  ResponseEntity.ok(lists);
    }

}
